<?php

include_once "src/controller/dbmanagement/UserDBManagement.php";
include_once "src/controller/blmanagement/AuthorizationAccountManagement.php";


/**
 * It includes all the functions for the management of the business logic
 * of the User Accounts.
 */
class UserAccountManagement {

    /**
     *  Returns User Object with user information in it if the user table contains a
     *  user with the User Id of $userId.
     */
    public static function getUserById($userId) {
        try {
            $user = new User();
            $user = UserDBManagement::findByUserId($userId);
            return $user;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }

    }

    /**
     *  Returns User Object with user information in it if the user table contains a
     *  user with the name of $userName and password of $pswd.
     */

    public static function getUserByEmail($email) {
        try {
            $user = new User();
            $user = UserDBManagement::findByLoginId($email);
            return $user;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }

    }

    /**
     *  Returns true if database successfully completes delete on
     *  user with the User Id of $userId else retruns false.
     */
    public static function removeByUserId($userId) {
        try {
            $result = UserDBManagement::deleteByUserId($userId);
            return $result;
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
    }

    /**
     * Returns array indexed by userName.
     * Returns empty array if there are no users.
     */
    public static function getAllUsers() {
        try {
            $userArray = UserDBManagement::findAllUsers();
            return $userArray;
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }

    }

    /**
     *  Returns true if database successfully completes update on
     *  user with the User Id of $userId else retruns false.
     */
    public static function changeUserAccount($user) {
        try {
            $userId=$user->getUserId();
           $oldUserInfo=UserDBManagement::findByUserId($userId);
           $result = UserDBManagement::updateUserAccount($user);
           if ($result)
           {
               $newPswd=$user->getPswd();
               $newEmail=$user->getEmail();
               $oldEmail= $oldUserInfo->getEmail();
               AuthorizationAccountManagement::changeAuthorizationAccount($newPswd, $newEmail, $oldEmail);
           }
           return $result;
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
    }

    /**
     *   Adds a user with the data in $user as identified by the User
     *   the User ID in $user.
     *   Returns false if user already exists or true if transaction complete
     *   Throws DBExeption if insert is unsuccessful.
     */
    public static function addUser($user) {
        try
        {
                $email = $user->getEmail();
                $pswd = $user->getPswd();
                if(AuthorizationAccountManagement::register($email,$pswd))
                {
                    $result = UserDBManagement::insertUserAccount($user);
                    return $result;
                }
                return FALSE;
        }
        catch (DBException $e) {
            throw $e;
        }
        catch (DBConnectionException $e) {
            throw $e;
        }
    }
}
?>
